﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCBug.Models
{
    public class TaskRepositry
    {
        private MVCBugDataContext _datacontext;

        public TaskRepositry(MVCBugDataContext datacontext)
        {
            _datacontext = datacontext;
        }


        public IEnumerable<Tasks> GetTasks()
        {
            return _datacontext.Tasks.OrderBy(t => t.CreatedOn);
        }

        public Tasks GetTask(int taskId)
        {
            return _datacontext.Tasks.SingleOrDefault(t => t.TaskID == taskId);
        }

        public Tasks CreateTask(string title, string text)
        {
            Tasks T = new Tasks { Title = title, Text = text };
            _datacontext.Tasks.InsertOnSubmit(T);
            _datacontext.SubmitChanges();
            return T;
        }

        public void UpdateTask(Tasks t)
        {
            Tasks DBTask = GetTask(t.TaskID);
            DBTask.AssignedOn = t.AssignedOn;
            DBTask.Title = t.Title;
            DBTask.Text = t.Text;
            _datacontext.SubmitChanges();
        }

        public void DeleteTask(int taskId)
        {
            Tasks t = GetTask(taskId);
            _datacontext.Tasks.DeleteOnSubmit(t);
            _datacontext.SubmitChanges();
        }
    }
}